home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / Demos / AirHockey / cAudioFile.cls < prev    next >
Encoding:
Visual Basic class definition  |  2001-10-08  |  2.4 KB  |  80 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "cAudioFile"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. 'Here we will control a 'set' of audio files
  17. Private mlNumSounds As Long
  18. Private dmSegments() As DirectMusicSegment8
  19. Private moPath As DirectMusicAudioPath8
  20.  
  21. Public Sub InitSounds(dmPerf As DirectMusicPerformance8, dmLoader As DirectMusicLoader8, ByVal sPath As String, ByVal sFirstPart As String, ByVal sExtenstion As String, Optional fSingleFile As Boolean = False)
  22.     Dim sFile As String
  23.     Dim lCount As Long
  24.     
  25.     'Here we will take a 'group' of files (that group could only be 1 file)
  26.     'and load them into our array
  27.     Set moPath = dmPerf.GetDefaultAudioPath
  28.     lCount = 1
  29.     If fSingleFile Then
  30.         sFile = Dir$(sPath & sFirstPart & sExtenstion)
  31.     Else
  32.         sFile = Dir$(sPath & sFirstPart & format$(CStr(lCount), "00") & sExtenstion)
  33.     End If
  34.     Do While sFile <> vbNullString
  35.         ReDim Preserve dmSegments(1 To lCount)
  36.         Set dmSegments(lCount) = dmLoader.LoadSegment(sPath & sFile)
  37.         dmSegments(lCount).Download moPath
  38.         lCount = lCount + 1
  39.         If fSingleFile Then
  40.             sFile = vbNullString
  41.         Else
  42.             sFile = Dir$
  43.         End If
  44.     Loop
  45.     mlNumSounds = lCount - 1
  46. End Sub
  47.  
  48. Public Sub Play(dmPerf As DirectMusicPerformance8)
  49.     Dim lRnd As Long
  50.     
  51.     'Pick a valid sound randomly and play it
  52.     Randomize
  53.     lRnd = CLng(Rnd * mlNumSounds) + 1
  54.     Do While lRnd < 1 Or lRnd > mlNumSounds
  55.         lRnd = CLng(Rnd * mlNumSounds) + 1
  56.     Loop
  57.     dmPerf.PlaySegmentEx dmSegments(lRnd), DMUS_SEGF_SECONDARY, 0
  58.  
  59. End Sub
  60.  
  61. Private Sub Class_Initialize()
  62.     'This should already have happened for us from VB, but just in case
  63.     Erase dmSegments
  64.     mlNumSounds = 0
  65. End Sub
  66.  
  67. Private Sub Class_Terminate()
  68.     Dim lCount As Long
  69.     'Let's clean everything up
  70.     For lCount = 1 To mlNumSounds
  71.         'Unload and release all the segments
  72.         If Not (dmSegments(lCount) Is Nothing) Then
  73.             dmSegments(lCount).Unload moPath
  74.             Set dmSegments(lCount) = Nothing
  75.         End If
  76.     Next
  77.     'Clear up any data left over
  78.     Erase dmSegments
  79. End Sub
  80.